package guoanmaker.provider.business.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.provider.business.model.PersonalCollection;
import guoanmaker.provider.business.model.PersonalUser;
import guoanmaker.provider.business.model.ProvideCity;
import guoanmaker.provider.business.model.ProvideCommodity;
import guoanmaker.provider.business.modelnobind.PersonalCollectionMove;
import guoanmaker.provider.business.modelnobind.PersonalCollectionUser;
import guoanmaker.provider.business.repository.PersonalCollectionRepository;

@Service
public class PersonalCollectionService implements PersonalCollectionServiceInterface {

	@Autowired
	private PersonalCollectionRepository personalCollectionRepository;
	
	@PersistenceContext
	private EntityManager manager;
	
	
	public PersonalCollectionMove insertPersonalCollection(String personalUserId,String  provideCommodityId) {
		List<PersonalCollection> list=personalCollectionRepository.findByPersonalUser_idIsAndProvideCommodity_idIs(personalUserId,provideCommodityId);
		PersonalCollection p=null;
		if(list==null||(list!=null&&list.size()==0)){
			PersonalCollection personalCollection=new PersonalCollection();
			PersonalUser personalUser=new PersonalUser();
			personalUser.setId(personalUserId);
			ProvideCommodity provideCommodity=new ProvideCommodity();
			provideCommodity.setId(provideCommodityId);
			personalCollection.setPersonalUser(personalUser);
			personalCollection.setProvideCommodity(provideCommodity);
			personalCollection.setCollectionTime(new Date());
			p=personalCollectionRepository.save(personalCollection);
		}
		else{
			p=list.get(0);
		}
		PersonalCollectionMove per=new PersonalCollectionMove();
		per.setId(p.getId());
		return per;
	}

	@Override
	@Transactional
	public void deletePersonalCollection(String personalUserId,String  provideCommodityId) {
		String jpql = "delete from PersonalCollection p where p.personalUser.id=:personalUserId and p.provideCommodity.id=:provideCommodityId"; 
		Query query = manager.createQuery(jpql); 
		query.setParameter("personalUserId",personalUserId);
		query.setParameter("provideCommodityId", provideCommodityId);
		int reuslt=query.executeUpdate();
		System.out.println(reuslt);
	}

	@Override
	public Map<String, Object> selectPersonalCollectionPage(String userid, Integer page, Integer size) {
		Map<String,Object> map=new HashMap<String,Object>();
		String jpql="select new guoanmaker.provider.business.modelnobind.PersonalCollectionUser(pb.id, pc.id, pb.commodityName, pbu.businessName, "
				+"pb.finalPrice, pbu.companyCity,pb.synopsis,pb.commodity_smallimage,pb.salesVolume,pb.type) "
				+ "from PersonalCollection pc left join pc.provideCommodity pb left join pc.personalUser user left join pb.providerBusiness pbu  "
				+ "where user.id=:userid and pb.commodityState=2 order by pc.collectionTime asc";
		Query query = manager.createQuery(jpql);
		query.setParameter("userid", userid);
		int count=query.getResultList().size();
		query.setMaxResults(size);
		query.setFirstResult(page*size);
		List<PersonalCollectionUser> list=query.getResultList();
		for (int i = 0; i < list.size(); i++) {
			PersonalCollectionUser p=list.get(i);
			String jpql1="select new guoanmaker.provider.business.model.ProvideCity(pc.name) "
					+ "from ProvideCity pc where pc.code=:code";
			Query query1 = manager.createQuery(jpql1);
			query1.setParameter("code", p.getCompanyCity());
			List<ProvideCity> list1=query1.getResultList();
			if(list!=null&&list1.size()>0){
				ProvideCity provideCity= list1.get(0);
				p.setCompanyCity(provideCity.getName());
			}
			
			list.remove(i);
			list.add(i, p);
		}
		map.put("list", list);
		map.put("count", count);
		return map;
	}
	public List<PersonalCollectionUser> selectPersonalCollection(String userid) {
		String jpql="select new guoanmaker.provider.business.modelnobind.PersonalCollectionUser(pb.id, pc.id, pb.commodityName, pbu.businessName, "
				+"pb.finalPrice, pbu.companyCity,pb.synopsis,pb.commodity_smallimage,pb.salesVolume,pb.type) "
				+ "from PersonalCollection pc left join pc.provideCommodity pb left join pc.personalUser user left join pb.providerBusiness pbu  where user.id=:userid order by pc.collectionTime asc";
		Query query = manager.createQuery(jpql);
		query.setParameter("userid", userid);
		List<PersonalCollectionUser> list=query.getResultList();
		for (int i = 0; i < list.size(); i++) {
			PersonalCollectionUser p=list.get(i);
			String jpql1="select new guoanmaker.provider.business.model.ProvideCity(pc.name) "
					+ "from ProvideCity pc where pc.code=:code";
			Query query1 = manager.createQuery(jpql1);
			query1.setParameter("code", p.getCompanyCity());
			List<ProvideCity> list1=query1.getResultList();
			if(list!=null&&list1.size()>0){
				ProvideCity provideCity= list1.get(0);
				p.setCompanyCity(provideCity.getName());
			}
			list.remove(i);
			list.add(i, p);
		}
		return list;
	}

	
}
